package com.yamaikeji.system.service.impl.plan;

import com.yamaikeji.common.utils.DateUtils;
import com.yamaikeji.system.domain.plan.Plan;
import com.yamaikeji.system.domain.plan.Task;
import com.yamaikeji.system.mapper.plan.PlanMapper;
import com.yamaikeji.system.mapper.plan.TaskMapper;
import com.yamaikeji.system.service.plan.ITaskService;
import com.yamaikeji.system.util.DateUtil;
import com.yamaikeji.system.util.IdCreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * 任务Service业务层处理
 * 
 * @author yamaikeji
 * @date 2023-10-03
 */
@Service
public class TaskServiceImpl implements ITaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private PlanMapper planMapper;
    /**
     * 查询任务列表
     * 
     * @param task 任务
     * @return 任务
     */
    @Override
    public List<Task> selectTaskList(Task task) {
        return taskMapper.selectTaskList(task);
    }

    /**
     * 查询任务
     *
     * @param task 任务
     * @return 任务
     */
    @Override
    public Task selectTask(Task task) {
        return taskMapper.selectTask(task);
    }


    /**
     * 查询每天任务数
     *
     * @param task 任务
     * @return 任务集合
     */
    @Override
    public List<Task> selectDayTaskNum(Task task) {
        return taskMapper.selectDayTaskNum(task);
    }

    /**
     * @description 根据天数查询待办统计
     * @author 桑伟杰
     * @date 2024-11-27 15:00:57
     * @param task
     * @return
     */
    @Override
    public Task selectReportCount(Task task) {
        return taskMapper.selectReportCount(task);
    }

    /**
     * 根据天数查询待办统计
     *
     * @param task 任务
     * @return 任务集合
     */
    @Override
    public List<Task> selectReportChart(Task task) {
        return taskMapper.selectReportChart(task);
    }

    /**
     * @description 根据天数分页查询待办
     * @author 桑伟杰
     * @date 2024-11-27 13:59:41
     * @param task
     * @return
     */
    @Override
    public List<Task> selectReportTaskList(Task task) {
        return taskMapper.selectReportTaskList(task);
    }

    /**
     * 新增任务
     *
     * @param task 任务
     * @return 结果
     */
    @Override
    public int insertTask(Task task) {
        return taskMapper.insertTask(task);
    }

    @Override
    public int createTask(List<Plan> planList) {
        for (Plan p : planList) {
            LocalDate currentTime = LocalDate.now();
            if ("1".equals(p.getPlanTimeType())) {
                Task getTask = new Task();
                getTask.setPlanNo(p.getPlanNo());
                getTask.setUserNo(p.getUserNo());
                getTask.setTaskTime(p.getPlanTime());
                getTask = taskMapper.selectTask(getTask);
                if (getTask == null) {
                    Task task = new Task();
                    String taskNo = IdCreateUtil.generateNo("T");
                    task.setTaskNo(taskNo);
                    task.setPlanNo(p.getPlanNo());
                    task.setUserNo(p.getUserNo());
                    task.setTaskTime(p.getPlanTime());
                    task.setTaskStatus("0");
                    taskMapper.insertTask(task);
                }
            } else if ("2".equals(p.getPlanTimeType())) {
                List<String> planTimeList = Arrays.asList(p.getPlanMoreTime().split(","));
                for (int i = 0; i < planTimeList.size(); i++) {
                    Task getTask = new Task();
                    getTask.setPlanNo(p.getPlanNo());
                    getTask.setTaskTime(LocalDate.parse(planTimeList.get(i)));
                    getTask = taskMapper.selectTask(getTask);
                    if (getTask == null) {
                        Task task = new Task();
                        String taskNo = IdCreateUtil.generateNo("T");
                        task.setTaskNo(taskNo);
                        task.setPlanNo(p.getPlanNo());
                        task.setUserNo(p.getUserNo());
                        task.setTaskTime(LocalDate.parse(planTimeList.get(i)));
                        task.setTaskStatus("0");
                        taskMapper.insertTask(task);
                    }
                }
            } else if ("3".equals(p.getPlanTimeType())) {
                int i = 0;
                while (DateUtil.compareDate(p.getPlanBeginTime().plusDays(i), p.getPlanEndTime()) <= 0) {
                    Task getTask = new Task();
                    getTask.setPlanNo(p.getPlanNo());
                    getTask.setTaskTime(p.getPlanBeginTime().plusDays(i));
                    getTask = taskMapper.selectTask(getTask);
                    if (getTask == null) {
                        Task task = new Task();
                        String taskNo = IdCreateUtil.generateNo("T");
                        task.setTaskNo(taskNo);
                        task.setPlanNo(p.getPlanNo());
                        task.setUserNo(p.getUserNo());
                        task.setTaskTime(p.getPlanBeginTime().plusDays(i));
                        task.setTaskStatus("0");
                        taskMapper.insertTask(task);
                    }
                    i++;
                }
            } else if ("4".equals(p.getPlanTimeType())) {
                if ("1".equals(p.getPlanPeriodType())) {
                    for (int i = 0; i < 7; i++) {
                        Task getTask = new Task();
                        getTask.setPlanNo(p.getPlanNo());
                        getTask.setTaskTime(currentTime.plusDays(i));
                        getTask = taskMapper.selectTask(getTask);
                        if (getTask == null) {
                            Task task = new Task();
                            String taskNo = IdCreateUtil.generateNo("T");
                            task.setTaskNo(taskNo);
                            task.setPlanNo(p.getPlanNo());
                            task.setUserNo(p.getUserNo());
                            task.setTaskTime(currentTime.plusDays(i));
                            task.setTaskStatus("0");
                            taskMapper.insertTask(task);
                        }
                    }
                } else if ("2".equals(p.getPlanPeriodType())) {
                    List<String> days =  Arrays.asList(p.getPlanPeriodTime().split(","));
                    Map<String, String> dayMapping = new HashMap<>();
                    dayMapping.put("一", "1");
                    dayMapping.put("二", "2");
                    dayMapping.put("三", "3");
                    dayMapping.put("四", "4");
                    dayMapping.put("五", "5");
                    dayMapping.put("六", "6");
                    dayMapping.put("日", "7");
                    List<String> numDays = new ArrayList<>();
                    for (String day : days) {
                        if (dayMapping.containsKey(day)) {
                            numDays.add(dayMapping.get(day));
                        }
                    }
                    for (int i = 0; i < 7; i++) {
                        Integer week = currentTime.plusDays(i).getDayOfWeek().getValue();
                        if (numDays.contains(String.valueOf(week))) {
                            Task getTask = new Task();
                            getTask.setPlanNo(p.getPlanNo());
                            getTask.setTaskTime(currentTime.plusDays(i));
                            getTask = taskMapper.selectTask(getTask);
                            if (getTask == null) {
                                Task task = new Task();
                                String taskNo = IdCreateUtil.generateNo("T");
                                task.setTaskNo(taskNo);
                                task.setPlanNo(p.getPlanNo());
                                task.setUserNo(p.getUserNo());
                                task.setTaskTime(currentTime.plusDays(i));
                                task.setTaskStatus("0");
                                taskMapper.insertTask(task);
                            }
                        }
                    }
                } else if ("3".equals(p.getPlanPeriodType())) {
                    List<String> days =  Arrays.asList(p.getPlanPeriodTime().split(","));
                    for (int i = 0; i < 7; i++) {
                        Integer day = currentTime.plusDays(i).getDayOfMonth();
                        if (days.contains(String.valueOf(day))) {
                            Task getTask = new Task();
                            getTask.setPlanNo(p.getPlanNo());
                            getTask.setTaskTime(currentTime.plusDays(i));
                            getTask = taskMapper.selectTask(getTask);
                            if (getTask == null) {
                                Task task = new Task();
                                String taskNo = IdCreateUtil.generateNo("T");
                                task.setTaskNo(taskNo);
                                task.setPlanNo(p.getPlanNo());
                                task.setUserNo(p.getUserNo());
                                task.setTaskTime(currentTime.plusDays(i));
                                task.setTaskStatus("0");
                                taskMapper.insertTask(task);
                            }
                        }
                    }
                }
            }
        }
        return 0;
    }

    /**
     * 修改任务
     * 
     * @param task 任务
     * @return 结果
     */
    @Override
    public int updateTask(Task task) {
        task.setUpdateTime(DateUtils.getNowDate());
        return taskMapper.updateTask(task);
    }

    /**
     * 修改使用时间
     * @param task
     * @return
     */
    @Override
    public int updateUseTime(Task task) {
        return taskMapper.updateUseTime(task);
    }

    /**
     * 删除任务对象
     *
     * @param task
     * @return 结果
     */
    @Override
    public int deleteTask(Task task) {
        return taskMapper.deleteTask(task);
    }

    /**
     * 删除任务对象
     *
     * @return 结果
     */
    @Override
    public int deleteAllTask(Task task) {
        return taskMapper.deleteAllTask(task);
    }
}
